55. Infrastructure Automation

Note

The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.

Automating Infrastructure Management Services results in reducing the FTE as well as cumulatively getting better ROI using multiple tools, orchestrators, orchestration Engine , scripts and easy UI

55.1: Simple script for black-box integration test of console applications

This is a simple example on how you can automate tests for a console application that interact with standard input and standard output.

The tested application read and sum every new line and will provide the result after a single white line is provided.

The powershell script write "pass" when the output match.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$process = New-Object System.Diagnostics.Process
$process.StartInfo.FileName = ".\ConsoleApp1.exe"
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.RedirectStandardOutput = $true
$process.StartInfo.RedirectStandardInput = $true

if ( $process.Start() ) {
  # input
  $process.StandardInput.WriteLine("1");
  $process.StandardInput.WriteLine("2");
  $process.StandardInput.WriteLine("3");
  $process.StandardInput.WriteLine();
  $process.StandardInput.WriteLine();
  # output check
  $output = $process.StandardOutput.ReadToEnd()
  if ( $output ) {
    if ( $output.Contains("sum 6") ) {
      Write "pass"
    }
    else {
      Write-Error $output
    }
  }
  $process.WaitForExit()
}